package com.lb.multithread.study.concurrent;

/**
 * @version 1.0
 * @Author: Edward
 * @Date: 2021/1/27 11:33
 * @Description: 变量可见性
 */
public class VolatileVisibilityTest {
    private static volatile boolean initFlag = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            System.out.println("waiting for data...");
            while (!initFlag){}
            System.out.println("====> success");
        }).start();

        Thread.sleep(2000);

        new Thread(() -> {
            prepareData();
        }).start();
    }

    private static void prepareData() {
        System.out.println("prepare date begin ...");
        initFlag = true;
        System.out.println("prepare data end ...");
    }
}
